package cn.wolfcode.service.impl;


import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageResult;
import cn.wolfcode.domain.Result;
import cn.wolfcode.exception.NotLoginException;
import cn.wolfcode.mapper.EmpMapper;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.utils.JWTUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    @Override
    public PageResult list(String name, Integer gender, Date begin, Date end, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Emp> list = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) list;
        return new PageResult(p.getTotal(), p.getResult());

    }

    @Override
    public Emp getById(Long id) {
        return empMapper.getById(id);

    }

    @Override
    public void add(Emp emp) {
        emp.setCreateTime(new Date());
        emp.setUpdateTime(new Date());
         empMapper.add(emp);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(new Date());
        empMapper.update(emp);
    }

    @Override
    public void delete(List<Long> ids) {
            empMapper.delete(ids);
    }

    @Override
    public Result login(Emp loginemp) {
        Emp emp=empMapper.login(loginemp);
        if(emp!=null){
            Map<String,Object> map=new HashMap<>();
            map.put("id",emp.getId());
            map.put("username",emp.getUsername());

            return Result.success(JWTUtils.createToken(map));
        } throw new NotLoginException("在检查下代码错误呢加油 完成考试");
    }

}






